<?php

namespace App\Admin\Views\SwtProcurement;

use App\Models\SwtGood;
use App\Models\SwtProcurement;
use Dcat\Admin\Support\LazyRenderable;
use Throwable;

class SwtProcurementView extends LazyRenderable
{

    /**
     * Get the evaluated contents of the object.
     *
     * @return array|string
     * @throws Throwable
     */
    public function render()
    {
        $order_info = SwtProcurement::with(['goods'])->where('id', $this->payload['id']) -> first();


        $order_info -> procurement_total /= 100;
        $status = ['1'=>'供应商','2'=>'自采'];
        $order_info->supplice_type_name = $status[$order_info->supplice_type];
        $type = ['1'=>'预采购','2'=>'采购中','3'=>'采购完成','0'=>'关闭'];

        switch ($order_info->procurement_type) {
            case '1':
                $order_info->style = 'background:#21b978';
                break;
            case '2':
                $order_info->style = 'background:#3085d6';
                break;
            case '3':
                $order_info->style = 'background:#dda451';
                break;
            default:
                $order_info->style = 'background:#ea5455';
                break;
        }
        $order_info->procurement_type = $type[$order_info->procurement_type];
        foreach($order_info->goods as &$goods){
            $swt_goods = SwtGood::with('good_quantity')->where('id',$goods->good_id)->first();
            if($swt_goods != null){
                $goods->goods_name = $swt_goods->goods_name;
                if(empty($swt_goods->good_quantity)){
                    $goods->quantity_sum = 0;
                }else{
                    $sale = $swt_goods->good_quantity->sale_quantity;
                    $loss = $swt_goods->good_quantity->loss_quantity;
                    $normal = $swt_goods->good_quantity->normal_quantity;
                    $goods->quantity_sum = $sale + $loss + $normal;
                }
            }else{
                $goods->goods_name = '';
                $goods->goods_quantity = '';
            }
            $goods->goods_price /= 100;
            $goods->advance_price = bcdiv($goods->advance_price,100,2);
        }

        return admin_view('admin.procurement.detail', ['order_info' => $order_info]);
    }

}

